<?php
/**
 * 智能合同
 */
namespace App\Services;

require_once  dirname(dirname(__DIR__)).'/vendor/antcloud/AntCloudSDKCore/Config.php';
use antcloud\AntCloudSDKCore\AntCloudClient;

class AntchainService
{
    const ANTCHAIN_SUCCESS_CODE = 200;//成功状态
    /**
     * 智能合同日志记录
     * @param $message
     * @param array $request
     * @param array $response
     */
    public static function requestLog($message,$request=[],$response=[])
    {
        $content = [
            'request'=>is_object($request) ? object_array($request) : $request,
            'response'=>is_object($response) ? object_array($response) : $response,
        ];
        \Illuminate\Support\Facades\Log::debug($message,$content);
    }

    /**
     * 智能合同
     * @return AntCloudClient
     */
    public static function antChainClient()
    {
        $client = new AntCloudClient(
            'https://openapi.antchain.antgroup.com/gateway.do',
            'ACy4VGdEiUEiZbpM',
            'qQxKEoqUrU8qYiVH5JmCu33ASCt5n4fa'
        );
        return $client;
    }

    /**
     * 电子合同平台方的用户注册
     * @param $alipayRequest
     * @param $result
     * @return array
     */
    public static function userCreate($user_data){
        $flag = false;
        if(empty($user_data['idcard'])||empty($user_data['user_mobile'])||empty($user_data['user_name'])||empty($user_data['user_id'])){
            return ['status'=>$flag, 'message'=>'参数错误'];
        }
        $request = array(
            "user.id_number" => $user_data['idcard'],
            "user.id_type" => "CRED_PSN_CH_IDCARD",
            "user.mobile" => $user_data['user_mobile'],
            "user.name" => $user_data['user_name'],
            "user.user_id" => $user_data['user_id'],
            "user_type" => "PERSON",
            "method" => "twc.notary.contract.user.create",
            "version" => "1.0",
            "product_instance_id" => 'notary-api-prod',
            "region_name" => "CN-HANGZHOU-FINANCE",
        );
        $client = self::antChainClient();
        $response = $client->execute($request);
        if($response['result_code'] == self::ANTCHAIN_SUCCESS_CODE){
            $flag = true;
        }
        self::requestLog('用户注册',$request,$response);
        return ['status'=>$flag, 'message'=>$flag ? '成功' : $response['result_msg'],'data'=>$flag ? $response['user_id'] : $response];
    }

    /**
     * 修改用户注册信息
     * @param $alipayRequest
     * @param $result
     * @return array
     */
    public static function userUpdate($user_data){
        $flag = false;
        if(empty($user_data['user_mobile'])||empty($user_data['user_name'])||empty($user_data['user_id'])){
            return ['status'=>$flag, 'message'=>'参数错误'];
        }
        $request = array(
            "mobile" => $user_data['user_mobile'],
            "name" => $user_data['user_name'],
            "user_id" => $user_data['user_id'],
            "user_type" => "PERSON",
            "method" => "twc.notary.contract.user.update",
            "version" => "1.0",
            "product_instance_id" => 'notary-api-prod',
            "region_name" => "CN-HANGZHOU-FINANCE",
        );
        $client = self::antChainClient();
        $response = $client->execute($request);
        if($response['result_code'] == self::ANTCHAIN_SUCCESS_CODE){
            $flag = true;
        }
        self::requestLog('修改用户注册信息',$request,$response);
        return ['status'=>$flag, 'message'=>$flag ? '成功' : $response['result_msg'],'data'=>$flag ? $response : ''];
    }


    /**
     * 创建签署流程
     * @param $alipayRequest
     * @param $result
     * @return array
     */
    public static function signflowCreate($flow_data,$pay_rules,$order_id){
        $flag = false;
        if(empty($flow_data['file_id']) || empty($flow_data['ant_user_id']) || empty($pay_rules)){
            return ['status'=>$flag, 'message'=>'缺少参数'];
        }
        $request['auto_archive'] = 'true';
        $request['auto_deduction_force'] = 'true';
        $request['business_scene'] = '都来租租赁合同';
        $request['contract_sign_flow_config.notice_developer_url'] = 'https://api.qianbaozuji.com'.CommonService::NOTICE_XXZ_URL_ANTCHAIN;
        $request['contract_sign_flow_config.notice_type'] = '1';
            $request['contract_sign_flow_config.redirect_url'] = 'alipays://platformapi/startApp?appId=2021004105651180&page=pagesA/pages/paySuccess/paySuccess?orderId='.$order_id;
        $request['contract_sign_flow_config.sign_platform'] = '2';
        if(!empty($pay_rules)){
            $request = array_merge($request,$pay_rules);
        }
        $request['sign_platform'] = 'ALIPAY';
        $request['payer_tuid'] = $flow_data['ant_user_id'];
        $request['payee_tuid'] = 'XMAUNLUF';
        $request['sign_validity'] = (time()+259200)*1000;
        $request['method'] = 'twc.notary.contract.signflow.create';
        $request['version'] = '1.0';
        $request['product_instance_id'] = 'notary-api-prod';
        $request['region_name'] = 'CN-HANGZHOU-FINANCE';
        $client = self::antChainClient();
        $response = $client->execute($request);
        // echo '<pre>';
        // print_r($response);
        // dd(1);
        if($response['result_code'] == self::ANTCHAIN_SUCCESS_CODE){
            $flag = true;
        }
        self::requestLog('创建签署流程',$request,$response);
        return ['status'=>$flag, 'message'=>$flag ? '成功' : $response['result_msg'],'data'=>$flag ? $response['flow_id'] : ''];
    }

    /**
     * 向电子合同签署流程中添加待签署文档
     */
    public static function documentAdd($file_id,$flow_id){
        $flag = false;
        if(empty($file_id) || empty($flow_id)){
            return ['status'=>$flag, 'message'=>'缺少参数'];
        }
        $request['docs.1.file_id'] = $file_id;
        $request['docs.1.file_name'] = '都来租租赁合同.pdf';
        $request['flow_id'] = $flow_id;
        $request['method'] = 'twc.notary.contract.document.add';
        $request['version'] = '1.0';
        $request['product_instance_id'] = 'notary-api-prod';
        $request['region_name'] = 'CN-HANGZHOU-FINANCE';
        $client = self::antChainClient();
        $response = $client->execute($request);
        if($response['result_code'] == self::ANTCHAIN_SUCCESS_CODE){
            $flag = true;
        }
        self::requestLog('向电子合同签署流程中添加待签署文档',$request,$response);
        return ['status'=>$flag, 'message'=>$flag ? '成功' : $response['result_msg'],'data'=>$flag ? $response : ''];
    }


    /**
     * 添加电子合同签署方手动盖章签署区
     */
    public static function handsignfieldCreate($flow_data){
        $flag = false;
        if(empty($flow_data['file_id']) || empty($flow_data['flow_id']) || empty($flow_data['ant_user_id'])){
            return ['status'=>$flag, 'message'=>'缺少参数'];
        }
        $request['flow_id'] = $flow_data['flow_id'];
        $request['sign_fields.1.account_id'] = $flow_data['ant_user_id'];
        $request['sign_fields.1.file_id'] = $flow_data['file_id'];
        $request['sign_fields.1.auto_execute'] = 'true';
        $request['sign_fields.1.pos_page'] = '16';
        $request['sign_fields.1.pos_x'] = '190';
        $request['sign_fields.1.pos_y'] = '555';
        $request['method'] = 'twc.notary.contract.handsignfield.create';
        $request['version'] = '1.0';
        $request['product_instance_id'] = 'notary-api-prod';
        $request['region_name'] = 'CN-HANGZHOU-FINANCE';
        $client = self::antChainClient();
        $response = $client->execute($request);
        if($response['result_code'] == self::ANTCHAIN_SUCCESS_CODE){
            $flag = true;
        }
        self::requestLog('添加电子合同签署方手动盖章签署区',$request,$response);
        return ['status'=>$flag, 'message'=>$flag ? '成功' : $response['result_msg'],'data'=>$flag ? $response : ''];
    }

    /**
     * 添加电子合同签署方自动盖章签署区
     */
    public static function signfieldAdd($flow_data,$mer_ant_id){
        $flag = false;
        if(empty($flow_data['file_id']) || empty($flow_data['flow_id'])){
            return ['status'=>$flag, 'message'=>'缺少参数'];
        }

        $request['flow_id'] = $flow_data['flow_id'];
        $request['sign_fields.1.authorized_account_id'] = 'XMAUNLUF1677219996';
        $request['sign_fields.1.file_id'] = $flow_data['file_id'];
        $request['sign_fields.1.auto_execute'] = 'true';
        $request['sign_fields.1.pos_page'] = '16';
        $request['sign_fields.1.pos_x'] = '190';
        $request['sign_fields.1.pos_y'] = '460';
        $request['sign_fields.2.authorized_account_id'] = $mer_ant_id;
        $request['sign_fields.2.file_id'] = $flow_data['file_id'];
        $request['sign_fields.2.auto_execute'] = 'true';
        $request['sign_fields.2.pos_page'] = '16';
        $request['sign_fields.2.pos_x'] = '190';
        $request['sign_fields.2.pos_y'] = '691';
        $request['method'] = 'twc.notary.contract.signfield.add';
        $request['version'] = '1.0';
        $request['product_instance_id'] = 'notary-api-prod';
        $request['region_name'] = 'CN-HANGZHOU-FINANCE';
        $client = self::antChainClient();
        $response = $client->execute($request);
        // dd($response);
        $flag = false;
        if($response['result_code'] == self::ANTCHAIN_SUCCESS_CODE){
            $flag = true;
        }
        self::requestLog('添加电子合同签署方自动盖章签署区',$request,$response);
        return ['status'=>$flag, 'message'=>$flag ? '成功' : $response['result_msg'],'data'=>$flag ? $response : ''];
    }


    /**
     * 电子合同签署流程开启
     */
    public static function flowStart($flow_id){
        $flag = false;
        if(empty($flow_id)){
            return ['status'=>$flag, 'message'=>'缺少参数'];
        }
        $request['flow_id'] = $flow_id;
        $request['method'] = 'twc.notary.contract.flow.start';
        $request['version'] = '1.0';
        $request['product_instance_id'] = 'notary-api-prod';
        $request['region_name'] = 'CN-HANGZHOU-FINANCE';
        $client = self::antChainClient();
        $response = $client->execute($request);
        if($response['result_code'] == self::ANTCHAIN_SUCCESS_CODE){
            $flag = true;
        }
        self::requestLog('电子合同签署流程开启',$request,$response);
        return ['status'=>$flag, 'message'=>$flag ? '成功' : $response['result_msg'],'data'=>$flag ? $response : ''];
    }


    /**
     * 通过电子合同模板添加文档
     */
    public static function fileAdd($order_data){
        $flag = false;
        if(empty($order_data)){
            return ['status'=>$flag, 'message'=>'缺少参数'];
        }
        $request = array(
            "name" => "线上租赁协议",
            "simple_form_fields" => json_encode($order_data),
            "template_id" => config('antchain.TEMPLATE_ID'),
            "method" => "twc.notary.contract.file.add",
            "version" => "1.0",
            "product_instance_id" => 'notary-api-prod',
            "region_name" => "CN-HANGZHOU-FINANCE",
        );
        $client = self::antChainClient();
        $response = $client->execute($request);
        // echo '<pre>';
        // print_r($response);
        // dd(1);
        if($response['result_code'] == self::ANTCHAIN_SUCCESS_CODE){
            $flag = true;
        }
        self::requestLog('通过电子合同模板添加文档',$request,$response);
        return ['status'=>$flag, 'message'=>$flag ? '成功' : $response['result_msg'],'data'=>$flag ? $response['file_id'] : $response];
    }

    /**
     * 静默签署授权
     */
    public static function signAuth($account_id){
        $flag = false;
        if(empty($account_id)){
            return ['status'=>$flag, 'message'=>'缺少参数'];
        }
        $request = array(
            "account_id" => $account_id,
            "method" => "twc.notary.contract.sign.auth",
            "version" => "1.0",
            "product_instance_id" => 'notary-api-prod',
            "region_name" => "CN-HANGZHOU-FINANCE",
        );
        $client = self::antChainClient();
        $response = $client->execute($request);
        // dd($response);
        if($response['result_code'] == self::ANTCHAIN_SUCCESS_CODE){
            $flag = true;
        }
        self::requestLog('静默签署授权',$request,$response);
        return ['status'=>$flag, 'message'=>$flag ? '成功' : $response['result_msg'],'data'=>$flag ? $response : ''];
    }


    /**
     * 获取签署地址
     */
    public static function getSignurl($account_id,$flow_id){
        $flag = false;
        if(empty($account_id) || empty($flow_id)){
            return ['status'=>$flag, 'message'=>'缺少参数'];
        }
        $request = array(
            "account_id" => $account_id,
            "flow_id" => $flow_id,
            "method" => "twc.notary.contract.signurl.get",
            "version" => "1.0",
            "product_instance_id" => 'notary-api-prod',
            "region_name" => "CN-HANGZHOU-FINANCE",
        );
        $client = self::antChainClient();
        $response = $client->execute($request);
        // echo '<pre>';
        // print_r($response);
        // dd(1);
        if($response['result_code'] == self::ANTCHAIN_SUCCESS_CODE){
            $flag = true;
        }
        self::requestLog('获取签署地址',$request,$response);
        return ['status'=>$flag, 'message'=>$flag ? '成功' : $response['result_msg'],'data'=>$flag ? $response : ''];
    }

    /**
     * 电子合同流程文档下载
     */
    public static function download($flow_id){
        $flag = false;
        if(empty($flow_id)){
            return ['status'=>$flag, 'message'=>'缺少参数'];
        }
        $request = array(
            "flow_id" => $flow_id,
            "method" => "twc.notary.contract.document.download",
            "version" => "1.0",
            "product_instance_id" => 'notary-api-prod',
            "region_name" => "CN-HANGZHOU-FINANCE",
        );
        $client = self::antChainClient();
        $response = $client->execute($request);
        // dd($response);
        if($response['result_code'] == self::ANTCHAIN_SUCCESS_CODE){
            $flag = true;
        }
        self::requestLog('电子合同流程文档下载',$request,$response);
        return ['status'=>$flag, 'message'=>$flag ? '成功' : $response['result_msg'],'data'=>$flag ? $response['docs'] : $response];
    }

    /**
     * 电子合同流程文档下载
     */
    public static function flowSave($flow_id){
        $flag = false;
        if(empty($flow_id)){
            return ['status'=>$flag, 'message'=>'缺少参数'];
        }
        $request = array(
            "flow_id" => $flow_id,
            "method" => "twc.notary.contract.flow.save",
            "version" => "1.0",
            "product_instance_id" => 'notary-api-prod',
            "region_name" => "CN-HANGZHOU-FINANCE",
        );
        $client = self::antChainClient();
        $response = $client->execute($request);
        // dd($response);
        if($response['result_code'] == self::ANTCHAIN_SUCCESS_CODE){
            $flag = true;
        }
        self::requestLog('手动归档签署流程',$request,$response);
        dd($response);
        return ['status'=>$flag, 'message'=>$flag ? '成功' : $response['result_msg'],'data'=>$flag ? $response['docs'] : $response];
    }



    /**
     * 通过文件上传方式创建电子合同签署模板
     */
    public static function templateCreate(){
        $file_path = '/www/wwwroot/www.doulaizu.com/都来租租赁合同20231225.pdf';
        $file_contents = file_get_contents($file_path);
        $md5_contents  = md5($file_contents, true);
        $base_contents = base64_encode($md5_contents);
        // $content_md5 = "QJxoNCVBy21krNjxX+FmSQ==";
        $content_md5 = $base_contents;

        $request = array(
            "content_md5" => $content_md5,
            "content_type" => "application/octet-stream",
            "convert2_pdf" => "false",
            "file_name" => "都来租租赁合同20230905.pdf",
            "method" => "twc.notary.contract.template.create",
            "version" => "1.0",
            "product_instance_id" => 'notary-api-prod',
            "region_name" => "CN-HANGZHOU-FINANCE",
        );
        $client = self::antChainClient();
        $response = $client->execute($request);
        // echo '<pre>';
        // dd($response);
        if($response['result_code'] == self::ANTCHAIN_SUCCESS_CODE){
            $template_id = $response['template_id'];
            $upload_url = $response['upload_url'];
            $fileContent = file_get_contents($file_path);
            $res = self::sendHttpPUT($upload_url,$content_md5,$fileContent);
            print_r($template_id);echo '状态';print_r($res);
        }
    }

    /**
     * 根据合同id查询订单id
     */
    public static function tradeIdsList($flow_id){
        $flag = false;
        if(empty($flow_id)){
            return ['status'=>$flag, 'message'=>'缺少参数'];
        }
        $request = array(
            "flow_id" => $flow_id,
            "page_index" => "1",
            "page_size" => "100",
            "method" => "twc.notary.contract.tradeids.list",
            "version" => "1.0",
            "product_instance_id" => 'notary-api-prod',
            "region_name" => "CN-HANGZHOU-FINANCE",
        );
        $client = self::antChainClient();
        $response = $client->execute($request);
        if($response['result_code'] == self::ANTCHAIN_SUCCESS_CODE){
            $flag = true;
        }
        self::requestLog('根据合同id查询订单id',$request,$response);
        return ['status'=>$flag, 'message'=>$flag ? '成功' : $response['result_msg'],'data'=>$flag ? $response['out_trade_no'] : $response];
    }

    /**
     * 根据订单id查询订单代扣详情
     */
    public static function tradeDetail($flow_id,$out_trade_no){
        $flag = false;
        if(empty($flow_id) || empty($out_trade_no)){
            return ['status'=>$flag, 'message'=>'缺少参数'];
        }
        $request = array(
            "flow_id" => $flow_id,
            "out_trade_no" => $out_trade_no,
            "method" => "twc.notary.contract.tradedetail.query",
            "version" => "1.0",
            "product_instance_id" => 'notary-api-prod',
            "region_name" => "CN-HANGZHOU-FINANCE",
        );
        $client = self::antChainClient();
        $response = $client->execute($request);
        if($response['result_code'] == self::ANTCHAIN_SUCCESS_CODE){
            $flag = true;
        }
        self::requestLog('根据订单id查询订单代扣详情',$request,$response);
        return ['status'=>$flag, 'message'=>$flag ? '成功' : $response['result_msg'],'data'=>$flag ? $response['data'] : $response];
    }

    /**
     * 查询代扣规则
     */
    public static function payRule($flow_id){
        $flag = false;
        if(empty($flow_id)){
            return ['status'=>$flag, 'message'=>'缺少参数'];
        }
        $request = array(
            "flow_id" => $flow_id,
            "method" => "twc.notary.contract.payrule.list",
            "version" => "1.0",
            "product_instance_id" => 'notary-api-prod',
            "region_name" => "CN-HANGZHOU-FINANCE",
        );
        $client = self::antChainClient();
        $response = $client->execute($request);
        // dd($response);
        if($response['result_code'] == self::ANTCHAIN_SUCCESS_CODE){
            $flag = true;
        }
        self::requestLog('查询代扣规则',$request,$response);
        return ['status'=>$flag, 'message'=>$flag ? '成功' : $response['result_msg'],'data'=>$flag ? $response['response_data'] : $response];
    }

    /**
     * 取消智能合同剩余代扣条目
     */
    public static function paytradeCancel($flow_id){
        $flag = false;
        if(empty($flow_id)){
            return ['status'=>$flag, 'message'=>'缺少参数'];
        }
        $request = array(
            "flow_id" => $flow_id,
            "method" => "twc.notary.contract.paytrade.cancel",
            "version" => "1.0",
            "product_instance_id" => 'notary-api-prod',
            "region_name" => "CN-HANGZHOU-FINANCE",
        );
        $client = self::antChainClient();
        $response = $client->execute($request);
        if($response['result_code'] == self::ANTCHAIN_SUCCESS_CODE){
            $flag = true;
        }
        self::requestLog('取消智能合同剩余代扣条目',$request,$response);
        return ['status'=>$flag, 'message'=>$flag ? '成功' : $response['result_msg'],'data'=>$flag ? $response:''];
    }

    /**
     * 触发代扣
     */
    public static function paysingletradeExec($flow_id,$payIndex){
        $flag = false;
        if(empty($flow_id) || empty($payIndex)){
            return ['status'=>$flag, 'message'=>'缺少参数'];
        }
        $request = array(
            "flowId" => $flow_id,
            "payIndex" => $payIndex,
            "method" => "twc.notary.contract.pay.exec",
            "version" => "1.0",
            "product_instance_id" => 'notary-api-prod',
            "region_name" => "CN-HANGZHOU-FINANCE",
        );
        $client = self::antChainClient();
        $response = $client->execute($request);
        if($response['result_code'] == self::ANTCHAIN_SUCCESS_CODE){
            $flag = true;
        }
        self::requestLog('触发代扣',$request,$response);
        return ['status'=>$flag, 'message'=>$flag ? '成功' : $response['result_msg'],'data'=>$flag ? $response:''];
    }

    /**
     * 取消某一期的代扣规则
     */
    public static function paysingletradeCancel($flow_id,$cancel_out_order_no){
        $flag = false;
        if(empty($flow_id) || empty($cancel_out_order_no)){
            return ['status'=>$flag, 'message'=>'缺少参数'];
        }
        $request = array(
            "flow_id" => $flow_id,
            "cancel_out_order_no" => $cancel_out_order_no,
            "method" => "twc.notary.contract.paysingletrade.cancel",
            "version" => "1.0",
            "product_instance_id" => 'notary-api-prod',
            "region_name" => "CN-HANGZHOU-FINANCE",
        );
        $client = self::antChainClient();
        $response = $client->execute($request);
        if($response['result_code'] == self::ANTCHAIN_SUCCESS_CODE){
            $flag = true;
        }
        self::requestLog('取消某一期的代扣规则',$request,$response);
        return ['status'=>$flag, 'message'=>$flag ? '成功' : $response['result_msg'],'data'=>$flag ? $response:''];
    }


    /**
     * 商户根据订单id退款
     */
    public static function merchantRefundCreate($flow_id,$out_request_no,$out_trade_no,$refund_amount){
        $flag = false;
        if(empty($flow_id) || empty($out_request_no) || empty($out_trade_no) || empty($refund_amount)){
            return ['status'=>$flag, 'message'=>'缺少参数'];
        }
        $request = array(
            "flow_id" => $flow_id,
            "out_request_no" => $out_request_no,
            "out_trade_no" => $out_trade_no,
            "refund_amount" => intval($refund_amount),
            "method" => "twc.notary.contract.merchantrefund.create",
            "version" => "1.0",
            "product_instance_id" => 'notary-api-prod',
            "region_name" => "CN-HANGZHOU-FINANCE",
        );
        $client = self::antChainClient();
        $response = $client->execute($request);
        // dd($response);
        if($response['result_code'] == self::ANTCHAIN_SUCCESS_CODE){
            $flag = true;
        }
        self::requestLog('商户根据订单id退款',$request,$response);
        return ['status'=>$flag, 'message'=>$flag ? '成功' : $response['result_msg'],'data'=>$flag ? $response:''];
    }

    /**
     * 代扣规则状态查询
     */
    public static function contractTradeStatusQuery($outTradeNo){
        $flag = false;
        if(empty($outTradeNo)){
            return ['status'=>$flag, 'message'=>'缺少参数'];
        }
        $request = array(
            "outTradeNo" => $outTradeNo,
            "method" => "twc.notary.contract.tradestatus.query",
            "version" => "1.0",
            "product_instance_id" => 'notary-api-prod',
            "region_name" => "CN-HANGZHOU-FINANCE",
        );
        $client = self::antChainClient();
        $response = $client->execute($request);
         dd($response);
        if($response['result_code'] == self::ANTCHAIN_SUCCESS_CODE){
            $flag = true;
        }
        self::requestLog('代扣规则状态查询',$request,$response);
        return ['status'=>$flag, 'message'=>$flag ? '成功' : $response['result_msg'],'data'=>$flag ? $response:''];
    }

    /**
     * 查询代扣退款
     */
    public static function contractRefundQuery($out_request_no){
        $flag = false;
        if(empty($out_request_no)){
            return ['status'=>$flag, 'message'=>'缺少参数'];
        }
        $request = array(
            "refundId" => $out_request_no,
            "method" => "twc.notary.contract.refund.query",
            "version" => "1.0",
            "product_instance_id" => 'notary-api-prod',
            "region_name" => "CN-HANGZHOU-FINANCE",
        );
        $client = self::antChainClient();
        $response = $client->execute($request);
        // dd($response);
        if($response['result_code'] == self::ANTCHAIN_SUCCESS_CODE){
            $flag = true;
        }
        self::requestLog('查询代扣退款',$request,$response);
        return ['status'=>$flag, 'message'=>$flag ? '成功' : $response['result_msg'],'data'=>$flag ? $response:''];
    }

    /**
     * 查询合同签署情况
     */
    public static function contractFlowQuery($flow_id){
        $flag = false;
        if(empty($flow_id)){
            return ['status'=>$flag, 'message'=>'缺少参数'];
        }
        $request = array(
            "flow_id" => $flow_id,
            "method" => "twc.notary.contract.flow.query",
            "version" => "1.0",
            "product_instance_id" => 'notary-api-prod',
            "region_name" => "CN-HANGZHOU-FINANCE",
        );
        $client = self::antChainClient();
        $response = $client->execute($request);
        // dd($response);
        if($response['result_code'] == self::ANTCHAIN_SUCCESS_CODE){
            $flag = true;
        }
        self::requestLog('查询合同签署情况',$request,$response);
        return ['status'=>$flag, 'message'=>$flag ? '成功' : $response['result_msg'],'data'=>$flag ? $response:''];
    }



    /**
     * 电子合同平台方注册
     */
    public static function platformCreate(){
        $request = array(
            "creator.id_number" => '330381198507130013',
            "creator.id_type" => 'CRED_PSN_CH_IDCARD',
            "creator.name" => '瑞安市诺明网络科技有限公司',
            "creator.user_id" => 'TXZ0001',
            "platform.id_number" => '91330381MAC65FWH5X',
            "platform.id_type" => 'CRED_ORG_USCC',
            "platform.legal_person" => '吴力军',
            "platform.legal_person_id" => '330381198507130013',
            "platform.name" => '瑞安市诺明网络科技有限公司',
            "platform.organization_id" => '91330381MAC65FWH5X',
            "platform.org_type" => 'ENTERPRISE',
            "method" => "twc.notary.contract.platform.create",
            "version" => "1.0",
            "product_instance_id" => 'notary-api-prod',
            "region_name" => "CN-HANGZHOU-FINANCE",
        );
        $client = self::antChainClient();
        $response = $client->execute($request);
        // dd($response);
        $flag = false;
        if($response['result_code'] == self::ANTCHAIN_SUCCESS_CODE){
            $flag = true;
        }
        self::requestLog('电子合同平台方注册',$request,$response);
        return ['status'=>$flag, 'message'=>$flag ? '成功' : $response['result_msg'],'data'=>$flag ? $response:''];
    }

    /**
     * 蚂蚁链解约
     */
    public static function payUnbind($external_agreement_no,$user_tuid){
        $flag = false;
        if(empty($external_agreement_no)){
            return ['status'=>$flag, 'message'=>'缺少参数'];
        }
        $request['user_tuid'] = $user_tuid;
        $request['external_agreement_no'] = $external_agreement_no;
        $request['allow_unsign'] = 'true';
        $request['method'] = 'twc.notary.contract.pay.unbind';
        $request['version'] = '1.0';
        $request['product_instance_id'] = 'notary-api-prod';
        $request['region_name'] = 'CN-HANGZHOU-FINANCE';
        $client = self::antChainClient();
        $response = $client->execute($request);
        if($response['result_code'] == self::ANTCHAIN_SUCCESS_CODE){
            $flag = true;
        }
        var_dump($response);
        self::requestLog('蚂蚁链解约',$request,$response);
        return ['status'=>$flag, 'message'=>$flag ? '成功' : $response['result_msg'],'data'=>$flag ? $response : ''];
    }

    /**
     * 电子合同企业用户注册
     * @param $alipayRequest
     * @param $result
     * @return array
     */
    public static function merchatCreate($shopData){
        $flag = false;
        $request = array(
            "organization.id_number" => $shopData['companyid'],
            "organization.id_type" => 'CRED_ORG_USCC',
            "organization.name" => $shopData['mechanism'],
            "organization.organization_id" => $shopData['member_id'],
            "user.id_type" => "CRED_PSN_CH_IDCARD",
            "user.id_number" => $shopData['idcard'],
            "user.name" => $shopData['faname'],
            "user.user_id" => 'TM'.$shopData['id'],
            "user_type" => "ORGANIZATION",
            "method" => "twc.notary.contract.user.create",
            "version" => "1.0",
            "product_instance_id" => 'notary-api-prod',
            "region_name" => "CN-HANGZHOU-FINANCE",
        );
        $client = self::antChainClient();
        $response = $client->execute($request);
        // dd($response);
        $flag = false;
        if($response['result_code'] == self::ANTCHAIN_SUCCESS_CODE){
            $flag = true;
        }
        self::requestLog('用户注册',$request,$response);
        return ['status'=>$flag, 'message'=>$flag ? '成功' : $response['result_msg'],'data'=>$flag ? $response['organization_id'] : $response];
    }

    /**
     * 商户入驻直付通新接口
     */
    public static function merchantApply(){
        $data['external_id'] = 'XMAUNLUF686868';
        $data['name'] = '瑞安市诺明网络科技有限公司';
        $data['alias_name'] = '瑞安市诺明网络科技有限公司';
        $data['merchant_type'] = '01';
        $data['mcc'] = '4812';
        $data['in_door_images'][0] = '16561a95-c3e7-4e81-89bd-3e9b782a1ac4.jpg';
        $data['in_door_images'][1] = '9c5907ab-754a-4961-b877-0343221f6906.jpg';
        $data['cert_no'] = '91330381MAC65FWH5X';
        $data['cert_type'] = '201';
        $data['cert_image'] = 'c290f203-f242-4091-b81e-91560e814a21.jpg';
        $data['legal_name'] = '吴力军';
        $data['legal_cert_no'] = '330381198507130013';
        $data['legal_cert_front_image'] = '850e38a8-4fb0-4556-a98d-539558ac30f5.jpg';
        $data['legal_cert_back_image'] = 'e9085a9e-5f87-4ba1-81a6-7b176f5c5152.jpg';
        $data['business_address']['city_code'] = '330300';
        $data['business_address']['district_code'] = '330381';
        $data['business_address']['province_code'] = '330000';
        $data['business_address']['address'] = '浙江省温州市瑞安市玉海街道解放中路118号（后半间）';
        $data['service_phone'] = '19357616167';
        $data['contact_infos'][0]['name'] = '吴力军';
        $data['contact_infos'][0]['phone'] = '19357616167';
        $data['contact_infos'][0]['mobile'] = '19357616167';
        $data['contact_infos'][0]['tag'][0] = '06';
        $data['contact_infos'][0]['type'][0] = 'AGENT';
        $data['out_door_images'][0] = 'a1b1295f-af48-4e7f-9f0f-f028657d494f.jpg';
        $data['service'][0] = '当面付';
        $data['sign_time_with_isv'] = '2022-03-24';
        $data['alipay_logon_id'] = '19357616167@163.com';
        $data['sites'][0]['site_type'] = '06';
        $data['sites'][0]['site_url'] = 'www.qianbaozuji.com';
        $data['sites'][0]['site_name'] = '都来租官网';
        $data['binding_alipay_logon_id'] = '19357616167@163.com';
        $request = [
            "biz_content" => json_encode($data),
            // "agent_account_id" => "2088420982082937",
            "method" => "twc.notary.contract.merchant.apply",
            "version" => "1.0",
            "product_instance_id" => 'notary-api-prod',
            "region_name" => "CN-HANGZHOU-FINANCE",
        ];
        $client = self::antChainClient();
        $response = $client->execute($request);
        // dd($response);
        $flag = false;
        if($response['result_code'] == self::ANTCHAIN_SUCCESS_CODE){
            $flag = true;
        }
        self::requestLog('商户入驻直付通新接口',$request,$response);
        return ['status'=>$flag, 'message'=>$flag ? '成功' : $response['result_msg'],'data'=>$flag ? $response:''];
    }

    /**
     * 进件审核
     */
    public static function merchantChess(){
        $flag = false;
        $request = array(
            "orderId" => "2023022400502000000082073174",
            "method" => "twc.notary.contract.merchantorder.query",
            "version" => "1.0",
            "product_instance_id" => 'notary-api-prod',
            "region_name" => "CN-HANGZHOU-FINANCE",
        );
        $client = self::antChainClient();
        $response = $client->execute($request);
        // dd($response);
        if($response['result_code'] == self::ANTCHAIN_SUCCESS_CODE){
            $flag = true;
        }
        self::requestLog('进件审核进度',$request,$response);
        return ['status'=>$flag, 'message'=>$flag ? '成功' : $response['result_msg'],'data'=>$flag ? $response:''];
    }
    /**
     * 进件查询
     */
    public static function merchantSelect(){
        $flag = false;
        $request = array(
            "agentAccountId" => "XMAUNLUF",
            "method" => "twc.notary.contract.merchantzft.query",
            "version" => "1.0",
            "product_instance_id" => 'notary-api-prod',
            "region_name" => "CN-HANGZHOU-FINANCE",
        );
        $client = self::antChainClient();
        $response = $client->execute($request);
        // dd($response);
        if($response['result_code'] == self::ANTCHAIN_SUCCESS_CODE){
            $flag = true;
        }
        self::requestLog('进件审核查询',$request,$response);
        return ['status'=>$flag, 'message'=>$flag ? '成功' : $response['result_msg'],'data'=>$flag ? $response:''];
    }
    /**
     * 进件修改
     */
    public static function merchantEdit(){
        $data['smid'] = '2088520345237931';
        $data['name'] = '瑞安市诺明网络科技有限公司';
        $data['alias_name'] = '瑞安市诺明网络科技有限公司';
        $data['merchant_type'] = '01';
        $data['mcc'] = '4812';
        $data['in_door_images'][0] = '16561a95-c3e7-4e81-89bd-3e9b782a1ac4.jpg';
        $data['in_door_images'][1] = '9c5907ab-754a-4961-b877-0343221f6906.jpg';
        $data['cert_no'] = '91330381MAC65FWH5X';
        $data['cert_type'] = '201';
        $data['cert_image'] = 'c290f203-f242-4091-b81e-91560e814a21.jpg';
        $data['legal_name'] = '吴力军';
        $data['legal_cert_no'] = '330381198507130013';
        $data['legal_cert_front_image'] = '850e38a8-4fb0-4556-a98d-539558ac30f5.jpg';
        $data['legal_cert_back_image'] = 'e9085a9e-5f87-4ba1-81a6-7b176f5c5152.jpg';
        $data['business_address']['city_code'] = '330300';
        $data['business_address']['district_code'] = '330381';
        $data['business_address']['province_code'] = '330000';
        $data['business_address']['address'] = '浙江省温州市瑞安市玉海街道解放中路118号（后半间）';
        $data['service_phone'] = '19357616167';
        $data['contact_infos'][0]['name'] = '吴力军';
        $data['contact_infos'][0]['phone'] = '19357616167';
        $data['contact_infos'][0]['mobile'] = '19357616167';
        $data['contact_infos'][0]['tag'][0] = '06';
        $data['contact_infos'][0]['type'][0] = 'AGENT';
        $data['out_door_images'][0] = 'a1b1295f-af48-4e7f-9f0f-f028657d494f.jpg';
        $data['service'][0] = '当面付';
        $data['sign_time_with_isv'] = '2022-03-24';
        $data['alipay_logon_id'] = '19357616167@163.com';
        $data['sites'][0]['site_type'] = '06';
        $data['sites'][0]['site_url'] = 'www.qianbaozuji.com';
        $data['sites'][0]['site_name'] = '都来租官网';
        $data['binding_alipay_logon_id'] = '19357616167@163.com';
        $request = [
            "biz_content" => json_encode($data),
            // "agent_account_id" => "2088420982082937",
            "method" => "twc.notary.contract.merchant.update",
            "version" => "1.0",
            "product_instance_id" => 'notary-api-prod',
            "region_name" => "CN-HANGZHOU-FINANCE",
        ];
        $client = self::antChainClient();
        $response = $client->execute($request);
        // dd($response);
        if($response['result_code'] == self::ANTCHAIN_SUCCESS_CODE){
            $flag = true;
        }
        self::requestLog('进件审核查询',$request,$response);
        return ['status'=>$flag, 'message'=>$flag ? '成功' : $response['result_msg'],'data'=>$flag ? $response:''];
    }
    /**
     * 二级商户代理挂接接口
     */
    public static function merchantBind(){
        $flag = false;
        $request = array(
            "smid" => "2088520345237931",
            "cert_no" => "91330381MAC65FWH5X",
            "agent_account_id" => "XMAUNLUF",
            "method" => "twc.notary.contract.merchant.bind",
            "version" => "1.0",
            "product_instance_id" => 'notary-api-prod',
            "region_name" => "CN-HANGZHOU-FINANCE",
        );
        $client = self::antChainClient();
        $response = $client->execute($request);
        if($response['result_code'] == self::ANTCHAIN_SUCCESS_CODE){
            $flag = true;
        }
        self::requestLog('二级商户代理挂接接口',$request,$response);
        return ['status'=>$flag, 'message'=>$flag ? '成功' : $response['result_msg'],'data'=>$flag ? $response:''];
    }


    /**
     * 智能合同图片上传
     * 图片大小有限制，但是接口返回没提示
     */
    public static function merchantimageCreate(){
        $flag = false;
        $file = file_get_contents('./img/20230228104143.jpg');
        $request = array(
            "content" => base64_encode($file),
            "file_name" => "20230228104143.jpg",
            "method" => "twc.notary.contract.merchantimage.create",
            "version" => "1.0",
            "product_instance_id" => 'notary-api-prod',
            "region_name" => "CN-HANGZHOU-FINANCE",
        );
        $client = self::antChainClient();
        $response = $client->execute($request);
        if($response['result_code'] == self::ANTCHAIN_SUCCESS_CODE){
            $flag = true;
        }
        self::requestLog('智能合同图片上传',$request,$response);
        return ['status'=>$flag, 'message'=>$flag ? '成功' : $response['result_msg'],'data'=>$flag ? $response:''];
    }

    /**
     * 申请区块链合同回调加签key
     */
    public static function callbackkeyApply(){
        $flag = false;
        $request = array(
            "method" => "twc.notary.contract.callbackkey.apply",
            "version" => "1.0",
            "product_instance_id" => 'notary-api-prod',
            "region_name" => "CN-HANGZHOU-FINANCE",
        );
        $client = self::antChainClient();
        $response = $client->execute($request);
        // dd($response);
        if($response['result_code'] == self::ANTCHAIN_SUCCESS_CODE){
            $flag = true;
        }
        self::requestLog('申请区块链合同回调加签key',$request,$response);
        return ['status'=>$flag, 'message'=>$flag ? '成功' : $response['result_msg'],'data'=>$flag ? $response:''];
    }


    /**
     * 上传模板文件流
     */
    public static function sendHttpPUT($uploadUrls, $contentMd5, $fileContent){
        $header = array(
            'Content-Type:application/octet-stream',
            'Content-Md5:'.$contentMd5
        );
        $status = '';
        $curl_handle = curl_init();
        curl_setopt($curl_handle, CURLOPT_URL, $uploadUrls);
        curl_setopt($curl_handle, CURLOPT_FILETIME, true);
        curl_setopt($curl_handle, CURLOPT_FRESH_CONNECT, false);
        curl_setopt($curl_handle, CURLOPT_HEADER, true); // 输出HTTP头 true
        curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl_handle, CURLOPT_TIMEOUT, 5184000);
        curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 120);
        curl_setopt($curl_handle, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl_handle, CURLOPT_SSL_VERIFYHOST, false);

        curl_setopt($curl_handle, CURLOPT_HTTPHEADER, $header);
        curl_setopt($curl_handle, CURLOPT_CUSTOMREQUEST, 'PUT');

        curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $fileContent);
        $result = curl_exec($curl_handle);
        $status = curl_getinfo($curl_handle, CURLINFO_HTTP_CODE);

        if ($result === false) {
            $status = curl_errno($curl_handle);
            $result = 'put file to oss - curl error :' . curl_error($curl_handle);
        }
        curl_close($curl_handle);
        return $status;
    }

 /**
     * 投诉订单查询--1
     * @param $alipayRequest
     * @param $result
     * @return array
     */
    public static function complain($complainEventId){
        $flag = false;
        $request = array(
            "complainEventId" => $complainEventId,
            "method" => "twc.notary.contract.complain.query",
            "version" => "1.0",
            "product_instance_id" => config('antchain.PRODUCT_INSTANCE_ID'),
            "region_name" => "CN-HANGZHOU-FINANCE",
        );
        $client = self::antChainClient();
        $response = $client->execute($request);
        if($response['result_code'] == self::ANTCHAIN_SUCCESS_CODE){
            $flag = true;
        }
        self::requestLog('投诉订单查询',$request,$response);
        return ['status'=>$flag, 'message'=>$flag ? '成功' : $response['result_msg'],'data'=>$flag ? $response : ''];
    }
    /**
     * 投诉反馈图片上传--2
     * @param $alipayRequest
     * @param $result
     * @return array
     */
    public static function complainimage($fileName,$content){
        $flag = false;
        /*
            商家进行投诉反馈时，若需要附带图片，可通过此接口生成图片id。
        */
        $request = array(
            "fileName" => $fileName, //图片名称，支持的后缀格式：jpg、jpeg、png
            "content" => $content, //图片内容，base64
            "method" => "twc.notary.contract.complainimage.upload",
            "version" => "1.0",
            "product_instance_id" => config('antchain.PRODUCT_INSTANCE_ID'),
            "region_name" => "CN-HANGZHOU-FINANCE",
        );
        $client = self::antChainClient();
        $response = $client->execute($request);
        if($response['result_code'] == self::ANTCHAIN_SUCCESS_CODE){
            $flag = true;
        }
        self::requestLog('投诉订单查询',$request,$response);
        return ['status'=>$flag, 'message'=>$flag ? '成功' : $response['result_msg'],'data'=>$flag ? $response : ''];
    }
 /**
     * 投诉反馈--3
     * @param $alipayRequest
     * @param $result
     * @return array
     */
    public static function complainfeedback($complainEventId,$content,$imgurl=''){
        $flag = false;
        /*
            反馈类目ID
            00:使用体验保障金退款；
            02:通过其他方式退款;
            03:已发货;
            04:其他;
            05:已完成售后服务;
            06:非我方责任范围；
        */
        $request = array(
            "complainEventId" => $complainEventId,
            "feedbackCode" => '04', //反馈类目ID
            "feedbackContent" => $content, //反馈内容，字数不超过200个字
            "feedbackImages"=>$imgurl,//商家处理投诉时反馈凭证的图片id，多个用英文逗号隔开（图片id可以通过"商户上传处理图片"接口获取）
            "method" => "twc.notary.contract.complainfeedback.send",
            "version" => "1.0",
            "product_instance_id" => config('antchain.PRODUCT_INSTANCE_ID'),
            "region_name" => "CN-HANGZHOU-FINANCE",
        );
        $client = self::antChainClient();
        $response = $client->execute($request);
        if($response['result_code'] == self::ANTCHAIN_SUCCESS_CODE){
            $flag = true;
        }
        self::requestLog('投诉订单查询',$request,$response);
        return ['status'=>$flag, 'message'=>$flag ? '成功' : $response['result_msg'],'data'=>$flag ? $response : ''];
    }














}
